package com.sgb.member.view_model;

import com.sgb.member.di.repository.MemberRepertory;
import com.sgb.member.entity.MemberBean;
import com.sgb.member.entity.MemberProfileBean;
import com.sgb.member.ui.fragment.CompanyWebProfileFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;

public class CompanyWebProfileModel extends BaseViewModel {
    CompanyWebProfileFragment fragment;
    MemberRepertory repertory;

    public CompanyWebProfileModel(CompanyWebProfileFragment fragment, MemberRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    public void showOrHideDialog(boolean aBoolean) {
        if (aBoolean) {
            if (!DialogHelper.isShowing()) {
                DialogHelper.showProgressDialog(fragment.getActivity(), "数据获取中...");
            }
        } else {
            DialogHelper.dismissProgressDialog();
        }
    }

    public void getCompanyProfile(MemberBean bean) {
        showOrHideDialog(true);
        Map<String, Object> map = new HashMap<>();
        map.put("id", bean.getCompanyId());
        map.put("companyNo", bean.getNo());
        map.put("memberType", bean.getMemberUpdateLeverType()); // 1供方会员 2需方会员
        repertory.getApi().getCompanyWebProfile(map).enqueue(new ICallBack<BaseEntity<MemberProfileBean>>() {
            @Override
            public void onSuccess(BaseEntity<MemberProfileBean> data) {
                if (data != null && data.getData() != null) {
                    fragment.setMemberProfile(data.getData());
                } else {
                    fragment.showEmptyView();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<MemberProfileBean>> call, Throwable t) {
                super.onFailure(call, t);
                fragment.showEmptyView();
            }
        });
    }
}
